package com.lenovo.testreport.test.server.api.controller;

import com.lenovo.testreport.general.service.GeneralCaseService;
import com.lenovo.testreport.test.server.api.model.TestServiceApiCaseWithBLOBs;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author CJF
 * @date 2018/12
 * <p>
 * 主要对CASE表进行操作，取出常规CASE信息
 */

@Controller
@RequestMapping("/test/web/api/case")
public class CaseController {
    @Autowired
    @Qualifier("caseServiceImpl")
    private GeneralCaseService service;

    //通过Rid查找报告对应的CASE表
    @RequestMapping("/FindByRid")
    public @ResponseBody
    String FindByRid(HttpServletRequest request) {
        int id = Integer.valueOf(request.getParameter("rid"));
        String tablename = request.getParameter("tablename");
        List<TestServiceApiCaseWithBLOBs> case1 = service.findByRid(tablename, id);
        StringBuilder sb = new StringBuilder();
        sb.append("<div ceass=\"table\"><div class=\"table-column-group\">")
                .append("<div class=\"table-column\" style=\"width:15%\"></div><div class=\"table-column\" style=\"width:40%\"></div>")
                .append("<div class=\"table-column\" style=\"width:8%\"></div><div class=\"table-column\" style=\"width:5%\"></div>")
                .append("<div class=\"table-column\" style=\"width:8%\"></div>")
                .append("<div class=\"table-column\" style=\"width:5%\"></div></div><div class=\"table-header-group\">")
                .append("<ul class=\"table-row\"><li class=\"table-cell\">接口</li><li class=\"table-cell\">路径</li><li class=\"table-cell\">请求方式</li>")
                .append("<li class=\"table-cell\">状态</li><li class=\"table-cell\">状态码</li><li class=\"table-cell\">详情</li></ul></div>");
        Set<String> TypeSet = new HashSet<>();
        for (TestServiceApiCaseWithBLOBs e : case1
                ) {
            TypeSet.add(e.getType());
        }
        for (String type : TypeSet) {
            sb.append("<div class=\"table-row-group\"><ul class=\"table-row category\"><li class=\"table-cell\" style=\"font-weight:bold;\">")
                    .append(type).append("</li><li class=\"table-cell\"></li><li class=\"table-cell\"></li><li class=\"table-cell\">")
                    .append("</li><li class=\"table-cell\"></li><li class=\"table-cell\"></li></ul>");
            for (TestServiceApiCaseWithBLOBs e : case1
                    ) {

                if (e.getType().equals(type)) {
                    sb.append("<ul class=\"table-row ");
                    if ("failed".equals(e.getOrnot().toLowerCase())) {
                        sb.append("fail");
                    } else {
                        sb.append(e.getOrnot());
                    }
                    sb.append("\">");
                    sb.append("<li class=\"table-cell\">").append(e.getApi()).append("</li>");
                    sb.append("<li class=\"table-cell\">").append(e.getPath()).append("</li>");
                    sb.append("<li class=\"table-cell\">").append(e.getMethod()).append("</li>");
                    sb.append("<li class=\"table-cell\">").append(e.getOrnot()).append("</li>");
                    sb.append("<li class=\"table-cell\">").append(String.valueOf(e.getStatuscode())).append("</li>");
                    sb.append("<li class=\"table-cell\"><a onclick=\"FindDetailByCid('" + e.getCid() + "','" + tablename + "')\">").append("详情</a></li></ul>");
                }
            }
            sb.append("</div>");
        }
        return sb.toString();
    }


}
